package com.concurrence.count;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * CountDownLatch 计数，若state不等于0 await阻塞
 */
public class CountDownDemo {
    public static void main(String[] args) throws InterruptedException {
        // state：4
        CountDownLatch countDownLatch = new CountDownLatch(4);
        // 3个线程跑起来
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        for (int i = 0; i < 3; i++) {
            executorService.submit(() -> {
                countDownLatch.countDown();
                System.out.println(Thread.currentThread().getName() + "在执行" + ":" + countDownLatch.getCount());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        // 打印countDownLatch值
        System.out.println(countDownLatch.getCount());
        countDownLatch.await();
        executorService.shutdown();
    }


}
